package cn.iocoder.yudao.module.eval.service.profession;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.eval.controller.admin.profession.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.profession.EvalProfessionDO;
import cn.iocoder.yudao.module.eval.dal.mysql.profession.EvalProfessionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.eval.enums.ErrorCodeConstants.*;

/**
 * 职业工种 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class EvalProfessionServiceImpl implements EvalProfessionService {

    @Resource
    private EvalProfessionMapper professionMapper;

    @Override
    public Long createProfession(EvalProfessionSaveReqVO createReqVO) {
        // 校验职业代码唯一性
        validateProfessionCodeUnique(null, createReqVO.getProfessionCode());

        // 插入
        EvalProfessionDO profession = BeanUtils.toBean(createReqVO, EvalProfessionDO.class);
        // 显式设置deleted字段默认值
        profession.setDeleted(false);
        professionMapper.insert(profession);
        // 返回
        return profession.getId();
    }

    @Override
    public void updateProfession(EvalProfessionSaveReqVO updateReqVO) {
        // 校验存在
        validateProfessionExists(updateReqVO.getId());
        // 校验职业代码唯一性
        validateProfessionCodeUnique(updateReqVO.getId(), updateReqVO.getProfessionCode());

        // 更新
        EvalProfessionDO updateObj = BeanUtils.toBean(updateReqVO, EvalProfessionDO.class);
        professionMapper.updateById(updateObj);
    }

    @Override
    public void deleteProfession(Long id) {
        // 校验存在
        validateProfessionExists(id);
        // 删除
        professionMapper.deleteById(id);
    }

    private void validateProfessionExists(Long id) {
        if (professionMapper.selectById(id) == null) {
            throw exception(PROFESSION_NOT_EXISTS);
        }
    }

    private void validateProfessionCodeUnique(Long id, String professionCode) {
        EvalProfessionDO profession = professionMapper.selectByProfessionCode(professionCode);
        if (profession == null) {
            return;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的职业
        if (id == null) {
            throw exception(PROFESSION_CODE_DUPLICATE);
        }
        if (!profession.getId().equals(id)) {
            throw exception(PROFESSION_CODE_DUPLICATE);
        }
    }

    @Override
    public EvalProfessionDO getProfession(Long id) {
        return professionMapper.selectById(id);
    }

    @Override
    public List<EvalProfessionDO> getProfessionList(Collection<Long> ids) {
        return professionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<EvalProfessionDO> getProfessionPage(EvalProfessionPageReqVO pageReqVO) {
        return professionMapper.selectPage(pageReqVO);
    }

    @Override
    public EvalProfessionDO getProfessionByCode(String professionCode) {
        return professionMapper.selectByProfessionCode(professionCode);
    }

    @Override
    public List<EvalProfessionDO> getProfessionListByApprovalStatus(Integer approvalStatus) {
        return professionMapper.selectListByApprovalStatus(approvalStatus);
    }

    @Override
    public List<EvalProfessionDO> getProfessionListByCategory(String category) {
        return professionMapper.selectListByCategory(category);
    }

    @Override
    public void auditProfession(Long id, Integer approvalStatus, String approvalComments) {
        // 校验存在
        validateProfessionExists(id);

        // 更新审核状态
        EvalProfessionDO updateObj = new EvalProfessionDO();
        updateObj.setId(id);
        updateObj.setApprovalStatus(approvalStatus);
        updateObj.setApprovalComments(approvalComments);
        if (approvalStatus == 1) { // 审核通过
            updateObj.setApprovalDate(LocalDate.now());
        }
        professionMapper.updateById(updateObj);
    }

    @Override
    public List<EvalProfessionRespVO> matchProfessions(String keyword, String category) {
        // TODO: 实现匹配职业工种逻辑
        return List.of();
    }

    @Override
    public List<EvalProfessionRespVO> searchProfessions(String keyword) {
        // TODO: 实现搜索职业工种逻辑
        return List.of();
    }

    @Override
    public EvalProfessionStatisticsRespVO getProfessionStatistics() {
        // TODO: 实现获取职业工种统计逻辑
        return new EvalProfessionStatisticsRespVO();
    }

    @Override
    public void batchAuditProfessions(@Valid List<EvalProfessionAuditReqVO> auditReqVOList) {
        // TODO: 实现批量审核职业工种逻辑
    }

    @Override
    public void updateProfessionStatus(Long id, Integer status, String reason) {
        // TODO: 实现更新职业工种状态逻辑
    }

}
